package com.xm.linxiaogang.utils

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import android.widget.Toast
import com.example.baselibrary.R

/**
 * 自定义吐司
 */
class ToastUtil {
    private var toast: Toast? = null
    private var mTextView: TextView? = null

    companion object {
        private var context: Context? = null
        private var instance: ToastUtil? = null

        @Synchronized
        fun get(): ToastUtil? {
            if (instance == null) {
                instance = ToastUtil()
            }
            return instance;
        }

        fun init(mContext: Context) {
            context = mContext
        }
    }

    private fun toast(content: String?) {
        if (toast == null) {
            toast = Toast(context)
            val view = LayoutInflater.from(context).inflate(R.layout.base_toast, null)
            mTextView = view.findViewById<View>(R.id.tv_toast) as TextView
            toast?.setView(view)
            toast?.duration = Toast.LENGTH_SHORT
        }
        mTextView!!.text = content
        toast!!.show()
    }

    private fun toast(resId: Int) {
        val content = context?.getString(resId)
        toast(content)
    }
}